package com.sop.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sop.dao.WatchArrangementsDao;
import com.sop.model.RoleInfo;
import com.sop.model.WatchArrangements;
import com.sop.model.vo.WatchArrangementsVo;

import common.base.dao.IBaseDaoImpl;
import common.tools.StringTool;

@Repository("watchArrangementsDao")
public class WatchArrangementsDaoImpl extends IBaseDaoImpl<WatchArrangements,WatchArrangementsVo> implements WatchArrangementsDao{

	@Override
	public List<WatchArrangements> findByDate(Date date) {
		 Session session = super.sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from  WatchArrangements where DATE_FORMAT(time,'%Y-%m-%d') = :date");
		 query.setDate("date",date);
		return query.list();
	}

	@Override
	public void updateReplaceMan(WatchArrangements watchArrangements) {
		 Session session = super.sessionFactory.getCurrentSession();
		 Query   query = session.createQuery("update WatchArrangements t set t.replaceMan = :replaceMan where id = :id");
		 query.setString("replaceMan", watchArrangements.getReplaceMan());
		 query.setInteger("id", watchArrangements.getId());
		 query.executeUpdate();
		 session.flush();
	}
	
	
	@Override
	public Criteria getSearchCriteria(Session session,WatchArrangementsVo searchVo) {
		Criteria c = session.createCriteria(WatchArrangements.class).createAlias("person","c");  
        
        if(searchVo.getWatchArrangements()!=null){
        //检索条件  
            String name =  searchVo.getWatchArrangements().getPerson().getName();  
            if (StringTool.isStringAvaliable(name)) {  
                c.add(Restrictions.ilike("c.name", "%" + name + "%"));  
            }  
        }   
        return c;
	}

}
